package com.zmn.oms.admin.controller.conf.reply.question;

import com.google.common.collect.Lists;
import com.zmn.common.dictionary.DictModel;
import com.zmn.consts.GlobalConsts;
import com.zmn.erp.common.newbiz.dictionary.ProductDict;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.model.entity.conf.reply.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author linfeng
 * @description：回访问题配置
 * @since 2020/04/16 16:49
 * @modifier
 **/
@Controller
@RequestMapping("/conf/reply/question/")
public class ReplyQuestionListController extends ReplyQuestionBaseController {
    private Logger logger = LoggerFactory.getLogger(ReplyQuestionListController.class);

    /**
     * 描述: 回访问题列表
     *
     * @param query
     *            查询实体
     * @return ModelAndView
     * @author linfeng
     * @since 15:38 2020/04/16
     */
    @RequestMapping(value = "list")
    //@RequiresPermissions("/conf/reply/question/list.action")
    public ModelAndView list(@ModelAttribute ReplyQuestionQuery query) {
        ModelAndView mav = new ModelAndView("conf/reply/question/list");
        List<ReplyQuestion> items = replyQuestionService.listPageByQuery(query);
        // 回访类别
        List<ReplyCategory> categoryList = replyCategoryService.listByQuery(new ReplyCategoryQuery());
        List<ReplyAnswer> answerList = replyAnswerService.listByQuery(new ReplyAnswerQuery());
        Map<Integer, String> platMap =
            GlobalConsts.getPlatList().stream().collect(Collectors.toMap(item -> item.getKey(), item -> item.getValue(), (p, u) -> p));
        Map<Integer, String> categoryMap =
            categoryList.stream().collect(Collectors.toMap(item -> item.getCategId(), item -> item.getName(), (p, u) -> p));
        Map<Integer, String> answerMap =
            answerList.stream().collect(Collectors.toMap(item -> item.getAnswerId(), item -> item.getAnswer(), (p, u) -> p));
        mav.addObject("items", items);
        mav.addObject("vo", query);
        mav.addObject("bizTypeList", OrderConsts.getOrderBizTypeList());
        mav.addObject("platMap", platMap);
        mav.addObject("categoryMap", categoryMap);
        mav.addObject("answerMap", answerMap);

        List<DictModel> questionTypeList = Lists.newArrayList();
        questionTypeList.add(new DictModel(1, "单选"));
        questionTypeList.add(new DictModel(2, "多选"));
        questionTypeList.add(new DictModel(3, "手动输入"));

        mav.addObject("questionTypeList", questionTypeList);
        mav.addObject("returnVisitCategorys", categoryList);
        return mav;
    }

    /**
     * 功能描述 ：详情页面
     *
     * @param id
     * @return org.springframework.web.servlet.ModelAndView
     * @author linfeng
     * @since 2020/04/16
     * @modifier
     */
    @RequestMapping(value = "detail")
    //@RequiresPermissions("/conf/reply/question/detail.action")
    public ModelAndView detail(@RequestParam(value = "id", defaultValue = "0") Integer id) {
        ModelAndView mav = new ModelAndView("conf/reply/question/detail");
        ReplyQuestion replyQuestion = new ReplyQuestion();
        if (id == 0) {
            replyQuestion = new ReplyQuestion();
        } else {
            replyQuestion = Optional.ofNullable(replyQuestionService.findById(id)).orElse(replyQuestion);
        }
        if (replyQuestion.getSort() == null) {
            Integer sort = replyQuestionService.findMaxSort();
            sort = sort == null ? 1 : sort + 1;
            replyQuestion.setSort(sort);
        }
        // 回访类别，只显示启用状态的
        ReplyCategoryQuery categoryQuery = new ReplyCategoryQuery();
        categoryQuery.setStatus(GlobalConsts.YES);
        List<ReplyCategory> categoryList = replyCategoryService.listByQuery(categoryQuery);

        // 回访答案，只显示启用状态的
        ReplyAnswerQuery answerQuery = new ReplyAnswerQuery();
        answerQuery.setStatus(GlobalConsts.YES);
        List<ReplyAnswer> answerList = replyAnswerService.listByQuery(answerQuery);

        mav.addObject("question", replyQuestion);
        mav.addObject("returnVisitCategorys", categoryList);
        mav.addObject("returnVisitAnswers", answerList);
        mav.addObject("bizTypeList", OrderConsts.getOrderBizTypeList());
        return mav;
    }
}
